﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TDT.Land.Entity;
using TDT.Land.BusinessLogic;
using System.IO;

namespace TDT.Land.Web.Controllers
{
    public class EventController : BaseController
    {        
        // GET: /Event/
        [CustomAuthorize(Roles="3")]
        public ActionResult Index()
        {
            List<Sys_Event> lstEvent = AdminRepo.Instance.GetAllEvent();
            ViewBag.lstEvent = lstEvent;
            return View();
        }

        [CustomAuthorize(Roles = "2")]
        public ActionResult insertEvnet(string id) {

            Sys_Event modelEvent = new Sys_Event();
            string curentOpenLink = "";
            if (!string.IsNullOrEmpty(id))
            {
                modelEvent = AdminRepo.Instance.GetEventById(int.Parse(id));
                curentOpenLink = modelEvent.LinkOpen != null ? modelEvent.LinkOpen : "";

            }

            string buidingType = CategoryType.W_NEW_TYPE.ToString();
            var pro_buiding = AdminRepo.Instance.GetProBuildingByType(buidingType).OrderByDescending(m => m.DateCreate).ThenByDescending(p=>p.BuildingName).ToList();
            List<SelectListItem> lstSelectBaiViet = new List<SelectListItem>();
            lstSelectBaiViet.Add(new SelectListItem { Text = "---", Value = "/" });
            foreach (var BaiViet in pro_buiding)
            {
                SelectListItem sel_ItemBV = new SelectListItem();
                sel_ItemBV.Text = BaiViet.BuildingName.Length > 20 ? BaiViet.BuildingName.Substring(0, 17) + "..." : BaiViet.BuildingName;
                sel_ItemBV.Value = BaiViet.Code;
                if (curentOpenLink == BaiViet.Code)
                    sel_ItemBV.Selected = true;

                lstSelectBaiViet.Add(sel_ItemBV);
            }

            ViewBag.lstBaiViet = lstSelectBaiViet;

            return View("UC_Edit_News",modelEvent);
        }

        [CustomAuthorize(Roles = "2")]
        public ActionResult DeleteEvent(string id) {

            string saveTT = "00";
            bool statusSave = false;
            if (!string.IsNullOrEmpty(id))
                statusSave = AdminRepo.Instance.DeleteEvent(int.Parse(id), UserName);
            
            if (statusSave)
                saveTT = "01";

            return Content(saveTT);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult doSaveEvent(Sys_Event model) {
            string saveTT = "00";
            bool statusSave = false;
            if (model.id > 0)
                statusSave = AdminRepo.Instance.UpdateEvent(model, UserName);
            else
                statusSave = AdminRepo.Instance.InsertEvent(model, UserName);

            if (statusSave)
                saveTT = "01";

            return Content(saveTT);
        }

        public ActionResult ShowEventHome()
        {
            Sys_Event LandInfo = AdminRepo.Instance.showTopEvent();

            ViewBag.ShowEvent = LandInfo;
            return PartialView("US_UC_EventHome");
        
        }


        public string Upload()
        {
            var status = "";
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Uploads/"), fileName);
                    file.SaveAs(path);
                    status = fileName;
                }
            }
            return status;
        }


    }
}
